package com.jiutong.meipenggang.ui.activitys;


import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.entity.ProvinceAndCityBean;
import com.jiutong.meipenggang.http.model.interfaces.BindingBankCardBiz;
import com.jiutong.meipenggang.http.model.requests.UserInfoRequest;
import com.jiutong.meipenggang.http.present.BindingBankCardPresent;
import com.jiutong.meipenggang.http.subscribers.ProgressSubscriber;
import com.jiutong.meipenggang.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipenggang.utils.IOUtil;
import com.jiutong.meipenggang.utils.StringUtil;
import com.jiutong.meipenggang.views.dialog.DoNetWorkCityPicker;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author Veken
 */
public class BindingBankCardActivity extends BaseActivity implements BindingBankCardBiz {

    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.iv_bind_bank_card_choice)
    ImageView ivBindBankCardChoice;
    @Bind(R.id.et_binding_bank_card)
    EditText etBindingBankCard;
    @Bind(R.id.et_binding_bank_card_name)
    EditText etBindingBankCardName;
    @Bind(R.id.et_binding_bank_card_idcard)
    EditText etBindingBankCardIdcard;
    @Bind(R.id.et_binding_bank_card_phone)
    EditText etBindingBankCardPhone;
    @Bind(R.id.tv_binding_bank_card_choice_city)
    TextView tvBindingBankCardChoiceCity;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.btn_binding_bank_card_confirm)
    Button btnBindingBankCardConfirm;
    @Bind(R.id.activity_binding_bank_card)
    LinearLayout activityBindingBankCard;

    private BindingBankCardPresent bindingBankCardPresent;
    private UserInfoRequest userInfoRequest;
    private DoNetWorkCityPicker dialog;
    private String provinceCode;
    private String cityCode;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_binding_bank_card;
    }

    @Override
    protected void initView() {
        super.initView();

        tvCommonTitle.setText("绑定银行卡");

        //获取城市列表信息
        chooseCity();


//        //初始化倒计时
//        timerUtil = new TimerUtil(tvEtBindingBankCardGetverifyCode,60000);
//        timerUtil.setCountTimerEt();

        bindingBankCardPresent = new BindingBankCardPresent(this, this);
    }


    @OnClick({R.id.rl_common_title_back, R.id.iv_bind_bank_card_choice, R.id.btn_binding_bank_card_confirm, R.id.tv_binding_bank_card_choice_city})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.iv_bind_bank_card_choice:
                break;
            case R.id.btn_binding_bank_card_confirm:
                if (isRightAction()){
                    bindingBankCardPresent.bindingBankCard();
                }
                break;
            case R.id.tv_binding_bank_card_choice_city:  //选择省市
                showChooseCityDialog();
                break;
            default:
                break;
        }
    }

    private void showChooseCityDialog() {
        if (dialog == null) {
            dialog = new DoNetWorkCityPicker();
            dialog.setOnDialogClickListener(new DoNetWorkCityPicker.OnDialogClickListener() {
                @Override
                public void onBtnClick(String city, String area, int pCode, int cCode) {
                    tvBindingBankCardChoiceCity.setText(city + area);
                    tvBindingBankCardChoiceCity.setTextColor(Color.BLACK);
                    provinceCode = pCode+"";
                    cityCode = cCode+"";
                }
            });
        }
        dialog.show(getSupportFragmentManager(), "BindingBankCardActivity");
    }

    /**
     * 获取省市列表信息
     */
    private void chooseCity() {
        if (userInfoRequest == null) {
            userInfoRequest = new UserInfoRequest();
        }
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<ProvinceAndCityBean>() {
            @Override
            public void onNext(ProvinceAndCityBean o) {
                IOUtil.saveObject(mContext, Constants.CITY_LIST, o);
            }

            @Override
            public void onError(Throwable e) {

            }
        };
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("OPT", 103);
        userInfoRequest.getCityList(new ProgressSubscriber(onNextListener, mContext), hashMap);
    }

    /**
     * 操作是否正确
     *
     * @return
     */
    private boolean isRightAction() {

        if (TextUtils.isEmpty(getBankName())) {
            toastShortShow("请输入开户姓名");
            return false;
        }
        if (TextUtils.isEmpty(getIdCard())) {
            toastShortShow("请输入身份证号");
            return false;
        }
        if (TextUtils.isEmpty(getBankCardNum())) {
            toastShortShow("请输入银行卡号");
            return false;
        }
        if (TextUtils.isEmpty(tvBindingBankCardChoiceCity.getText().toString().trim())) {
            toastShortShow("请选择开户省市");
            return false;
        }

        if (TextUtils.isEmpty(getPhoneNum())) {
            toastShortShow("请输入手机号");
            return false;
        }
        if (!StringUtil.isMobileNumber(getPhoneNum())) {
            toastShortShow("您输入的手机号格式不对,请重新输入");
            return false;
        }
        return true;
    }


    @Override
    public String getPhoneNum() {
        return etBindingBankCardPhone.getText().toString().trim();
    }


    @Override
    public String getBankCardNum() {
        return etBindingBankCard.getText().toString().trim();
    }

    @Override
    public String getBankName() {
        return etBindingBankCardName.getText().toString().trim();
    }

    @Override
    public String getIdCard() {
        return etBindingBankCardIdcard.getText().toString().trim();
    }

    @Override
    public String getProvinceCode() {
        return provinceCode;
    }

    @Override
    public String getCityCode() {
        return cityCode;
    }

    @Override
    public void onSuccess(Object object) {
        BaseBean baseBean = (BaseBean) object;
        if(baseBean.getError()==-1){
            toastShortShow(baseBean.getMsg());
            finish();
        }else{
            toastShortShow(baseBean.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 104;
    }

}
